/*
 * Copyright 2004-2005 asoft
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）* 复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.asoft.cn/licenses
 */

package com.asoft.examonline.exammanager.model;

import java.io.Serializable;
import java.util.LinkedHashSet;
import java.util.Set;

import com.asoft.common.base.model.BaseObject;
import com.asoft.common.basedata.model.Type;
import com.asoft.common.util.random.RandomArray;
import com.asoft.examonline.define.QuestionTypeCode;
import com.asoft.examonline.exammanager.define.ExamQuestionStatus;
import com.asoft.examonline.exammanager.util.CodeUtil;
import com.asoft.examonline.exammanager.view.foreveryone.QuestionType2Dir;

/**
 * <p>Title: 试题</p>
 * <p>Description: TransferObject </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: asoft</p>
 * @ $Author: author $
 * @ $Date: 2004/12/17 13:54:17 $
 * @ $Revision: 1.4 $
 * @ created in 2007-2-20
 *
 */

public class ExamQuestion extends BaseObject
                       implements Serializable
{
        // 题目内容
        private String content;

        // 知识点
        private Set<Type> knowledgePoints;
        
        // 来源
        private Type source;
        
        // 难度
        private Type difficulty;     
        
        // 问题类型
        private Type questType;
        
        // 解决办法
        private String resolution;
        
        // 答案
        private Set<Result> results;
        
        private Set oldResults;
        
        private String authorName;
        
        // 状态
        private int status;

        /**
         * 构造函数
         */
        public ExamQuestion()
        {
                // for hibernate
        }

        public ExamQuestion(String code,
                            String content,
                            Set<Type> knowledgePoints,
                            Type source,
                            Type difficulty,
                            Type questType,
                            String authorName,
                            String resolution,
                            Set<Result> results,
                            int status)
        {
                super();
                this.code = code;
                this.content = content;
                this.knowledgePoints = knowledgePoints;
                this.source = source;
                this.difficulty = difficulty;
                this.questType = questType;
                this.resolution = resolution;
                this.authorName = authorName;
                this.results = results;
                this.status = status;
        }

        /**
         * @return content
         */
        public String getContent()
        {
                return content;
        }

        public String getNotNewLineContent()
        {
                String c = this.content;
                c = c.replaceAll("<p>", "");
                c = c.replaceAll("</p>", "");
                return c;
        }

        /**
         * @param content 要设置的 content
         */
        public void setContent(String content)
        {
                this.content = content;
        }

        /**
         * @return difficulty
         */
        public Type getDifficulty()
        {
                return difficulty;
        }

        /**
         * @param difficulty 要设置的 difficulty
         */
        public void setDifficulty(Type difficulty)
        {
                this.difficulty = difficulty;
        }

        /**
         * @return questType
         */
        public Type getQuestType()
        {
                return questType;
        }

        /**
         * @param questType 要设置的 questType
         */
        public void setQuestType(Type questType)
        {
                this.questType = questType;
        }

        /**
         * @return resolution
         */
        public String getResolution()
        {
                return resolution;
        }

        public String getNowNewLineResolution()
        {
                String c = this.resolution;
                c = c.replaceAll("<p>", "");
                c = c.replaceAll("</p>", "");
                return c;
        }

        /**
         * @param resolution 要设置的 resolution
         */
        public void setResolution(String resolution)
        {
                this.resolution = resolution;
        }

        /**
         * @return source
         */
        public Type getSource()
        {
                return source;
        }

        /**
         * @param source 要设置的 source
         */
        public void setSource(Type source)
        {
                this.source = source;
        }
        /**
         * @return results
         */
        public Set<Result> getResults()
        {
                return results;
        }
        
        public Set<Result> getRandomResults()
        {
                Set<Result> rss = new LinkedHashSet<Result>();
                int[] is = RandomArray.parse(this.results.size());
                for(int i = 0; i < is.length; i ++)
                {
                        rss.add((Result)this.results.toArray()[is[i]]);
                }
                return rss;
        }

        /**
         * @param results 要设置的 results
         */
        public void setResults(Set<Result> results)
        {
                for(Result rs : results)
                {
                        rs.setExamQuestion(this);
                }
                this.oldResults = this.results;
                this.results = results;
        }

        /**
         * @return oldResults
         */
        public Set getOldResults()
        {
                return oldResults;
        }

        /**
         * @return status
         */
        public int getStatus()
        {
                return status;
        }

        /**
         * @return knowledgePoints
         */
        public Set<Type> getKnowledgePoints()
        {
                return knowledgePoints;
        }

        /**
         * @param knowledgePoints 要设置的 knowledgePoints
         */
        public void setKnowledgePoints(Set<Type> knowledgePoints)
        {
                this.knowledgePoints = knowledgePoints;
        }

        /**
         * @param status 要设置的 status
         */
        public void setStatus(int status)
        {
                this.status = status;
        }
        
        public String getStatusName()
        {
                return ExamQuestionStatus.NAMES[this.status];
        }
        
        // 取得题型抽象代码
        public String getQuestTypeAbstractCode()
        {
                return CodeUtil.subjectQuestTypeCode2QuestType(this.getQuestType());
        }

        /**
         * @return authorName
         */
        public String getAuthorName()
        {
                return authorName;
        }

        /**
         * @param authorName 要设置的 authorName
         */
        public void setAuthorName(String authorName)
        {
                this.authorName = authorName;
        }
        
        // 是否主观题
        public boolean isSubjective()
        {
                return !(this.isSimpleSelect() || this.isMultielect());
        }
        
        public boolean isSimpleSelect()
        {
                return (QuestionType2Dir.toDir(this.questType).equals(QuestionTypeCode.SIMPLE_SELECT));
        }
        
        public boolean isMultielect()
        {
                return (QuestionType2Dir.toDir(this.questType).equals(QuestionTypeCode.MULTI_SELECT));
        }
        
        public String getKnowledgePoints4Html()
        {
                String html = "";
                for(Type kp : this.knowledgePoints)
                {
                        html = html + "<b>" + kp.getSuperType().getSuperType().getSuperType().getSuperType().getName() + "<b/> - " +
                               kp.getSuperType().getSuperType().getSuperType().getName() + " - " +
                               kp.getName() + "<br>";
                }
                return html;
        }
        
        public String getResults4Html()
        {
                String html = "";
                String[] indexs = new String[] {"A","B","C","D","E","F","G","H"};
                int i = 0;
                for(Result rs : this.results)
                {
                        html = html + "<div>" ;
                        if(this.results.size() > 1)
                        {
                                html = html+ "<b>" + indexs[i] + "</b>&nbsp;&nbsp;";
                                if(rs.isRight())
                                {
                                        html = html + "<font color='red'>正确</font>";
                                }
                        }
                        
                        html = html + rs.getContent()+ "</div>";
                        i ++;
                }
                return html;
        }

        /** 采用框架提供的操作日志必须填写的项目 */
        public void setModeldetail()
        {
                this.setModelFieldDetail("id",this.id);
                for(Type kp : this.knowledgePoints)
                {
                        this.setModelFieldDetail("知识点",kp.getName());
                }
                this.setModelFieldDetail("难度",this.getDifficulty().getName());
                this.setModelFieldDetail("题型",this.getQuestType().getName());
                this.setModelFieldDetail("来源",this.getSource().getName());
                this.setModelFieldDetail("命题人",this.authorName);
                this.setModelFieldDetail("编号",this.getCode());
                this.setModelFieldDetail("内容",this.content);
                for(Result rs : this.results)
                {
                        this.setModelFieldDetail("答案(" + rs.getRightErrorName() + ")",rs.getContent());
                }
                this.setModelFieldDetail("解析",this.resolution);
                this.setModelFieldDetail("状态",this.getStatusName());
        }
}
